<?php
/**
 * Created by PhpStorm.
 * User: dung
 * Date: 11/11/14
 * Time: 10:49 AM
 */

class Topic extends Eloquent
{
    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

    public function lyrics()
    {
        return $this->belongsToMany('Lyric', 'topicables', 'topic_id', 'lyric_id');
    }

    public function lesson()
    {
        return $this->belongsToMany('Lesson', 'topicables', 'topic_id', 'lesson_id');
    }

    public function videos()
    {
        return $this->belongsToMany('Video', 'topicables', 'topic_id', 'video_id');
    }

    public static function returnArray()
    {
        $key = 'Topic-Array-' . date('Ymd', time());

        $array = array();

        if (Cache::has($key))
            $array = Cache::get($key);
        else {
            $topics = Topic::select('id', 'title')->get();

            foreach ($topics as $topic) {
                $array[$topic->id] = $topic->title;
            }
            Cache::put($key, $array, 60);
        }
        return $array;
    }
} 